import Footer from '@/components/Footer';
//import { login } from '@/services/ant-design-pro/api';
import {
  LockOutlined,
  UserOutlined,
} from '@ant-design/icons';
import {
  LoginForm,
  ProFormText,
} from '@ant-design/pro-components';
import { message, Tabs } from 'antd';
import React, { useState } from 'react';
import {
  FormattedMessage,
  history,
  Link,
  SelectLang,
} from 'umi';
import styles from './index.less';
import {registerUser} from "@/services/ant-design-pro/api";
const Register: React.FC = () => {
  const [type, setType] = useState<string>('account');
  const handleSubmit = async (registerParams: API.RegisterParams) => {
    try {
      // 注册
      const id = await registerUser(registerParams);
      if (id >= 0) {
        message.success("注册成功");
        history.push("/user/login");
        return;
      }
      else{
        message.error("注册失败!");
      }
    } catch (error) {
      const defaultRegisterFailureMessage =  "注册失败";
      message.error(defaultRegisterFailureMessage);
    }
  };

  return (
    <div className={styles.container}>
      <div className={styles.lang} data-lang>
        {SelectLang && <SelectLang />}
      </div>
      <div className={styles.content}>
        <LoginForm
          submitter={
          {
            searchConfig:{
              submitText: '注册'
            }
          }
          }
          logo={<img alt="logo" src="/logo.svg" />}
          title="用户注册"
          initialValues={{
            autoLogin: true,
          }}
          onFinish={async (registerParam : API.RegisterParams) => {
            await handleSubmit(registerParam);
          }}
        >
          <Tabs activeKey={type} onChange={setType}>
            <Tabs.TabPane
              key="account"
              tab= "用户注册"
            />
          </Tabs>

          {type === 'account' && (
            <>
              <ProFormText
                name="userAccount"
                fieldProps={{
                  size: 'large',
                  prefix: <UserOutlined className={styles.prefixIcon} />,
                }}
                placeholder="请输入用户名"
                rules={[
                  {
                    required: true,
                    message: (
                      <FormattedMessage
                        id="pages.login.username.required"
                        defaultMessage="请输入用户名!"
                      />
                    ),
                  },
                ]}
              />
              <ProFormText.Password
                name="userPassword"
                fieldProps={{
                  size: 'large',
                  prefix: <LockOutlined className={styles.prefixIcon} />,
                }}
                placeholder="用户密码"
                rules={[
                  {
                    required: true,
                    message: (
                      <FormattedMessage
                        id="pages.login.password.required"
                        defaultMessage="请输入用户密码！"
                      />
                    ),
                  },
                ]}
              />
              <ProFormText.Password
                name="checkedUserPassword"
                fieldProps={{
                  size: 'large',
                  prefix: <LockOutlined className={styles.prefixIcon} />,
                }}
                placeholder="请重复密码"
                rules={[
                  {
                    required: true,
                    message: (
                      <FormattedMessage
                        id="pages.login.password.required"
                        defaultMessage="请确认密码！"
                      />
                    ),
                  },
                ]}
              />
            </>
          )}

          <div
            style={{
              marginBottom: 24,
            }}
          >
            <Link to="/user/login" style={{float: 'right'}}>去登录</Link>
          </div>
        </LoginForm>
      </div>
      <Footer />
    </div>
  );
};

export default Register;
